package com.css.zfzx.fwsjcj.modules.structure.verify;

import cn.afterturn.easypoi.excel.entity.result.ExcelVerifyHandlerResult;
import cn.afterturn.easypoi.handler.inter.IExcelVerifyHandler;
import org.springframework.util.StringUtils;

import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author
 * @program
 * @description
 * @create 2022/2/14 14:42
 **/
public class BrickwoodStructureCheck implements IExcelVerifyHandler {
    /**
     * 错误信息
     */
    private StringBuilder msg;
    /**
     * 省市区地区map
     */
    private HashMap<String, List> map;


    public BrickwoodStructureCheck(HashMap map) {
        this.map = map;
    }

    public BrickwoodStructureCheck() {
    }

    @Override
    public ExcelVerifyHandlerResult verifyHandler(Object o) {
        // 默认为true
        ExcelVerifyHandlerResult result = new ExcelVerifyHandlerResult(true);
        msg = new StringBuilder();
        boolean hasMsg = isHasMsg(o);
        if (hasMsg) {
            result.setSuccess(false);
            result.setMsg(msg.toString());
        }
        return result;
    }

    /**
     * 是否有错误消息
     */
    private boolean isHasMsg(Object o) {
        if (isFieldHasMsg(o)) {
            msg.append("填写错误");
            return true;
        }
        return false;
    }

    /**
     * 校验字段是否市下拉列表中的字典值
     */
    private boolean isFieldHasMsg(Object o) {
        boolean flag = false;
        // 所属省份
        String province = getFieldValueByFieldName("belongtoProvince", o);
        if (province != null && !map.containsKey(province)) {
            msg.append("所属省份(填写全名称,检查省市县关系),");
            flag = true;
        } else {
            String city = getFieldValueByFieldName("belongtoCity", o);
            // 所属地级市
            if (!"北京市".equals(province) && !"天津市".equals(province) && !"上海市".equals(province) && !"重庆市".equals(province)) {
                List<String> cities = map.get(province);
                if (city != null && cities != null && !cities.contains(city)) {
                    msg.append("所属地级市(填写全名称,省市关系不匹配),");
                    flag = true;
                } else {
                    // 所属区县
                    String area = getFieldValueByFieldName("belongtoArea", o);
                    List<String> areas = map.get(city);
                    if (area != null && areas != null && !areas.contains(area)) {
                        msg.append("所属区县(填写全名称,市县关系不匹配),");
                        flag = true;
                    }
                }
            }
            // 所属地级市为直辖市
            else {
                if (city != null && !province.equals(city)) {
                    msg.append("所属地级市(填写全名称,直辖市应与省份同名)");
                    flag = true;
                } else {
                    // 所属区县
                    String area = getFieldValueByFieldName("area", o);
                    List<String> areas = map.get(city);
                    if (area != null && areas != null && !areas.contains(area)) {
                        msg.append("所属区县(填写全名称,市县关系不匹配)");
                        flag = true;
                    }
                }
            }
        }

        // 建造年代(下拉选项)
        String buildingAgeField = getFieldValueByFieldName("buildingAge", o);
        Map buildingAge=new HashMap<>();
        buildingAge.put("70s以前","");
        buildingAge.put("70s","");
        buildingAge.put("80s","");
        buildingAge.put("90s","");
        buildingAge.put("00s","");
        buildingAge.put("10s","");
        boolean buildingAgeBoo = buildingAge.containsKey(buildingAgeField);
        if (buildingAgeField != null && !buildingAgeBoo) {
            msg.append("建造年代(下拉选项),");
            flag = true;
        }

        // 场地类型(下拉选项)
        String siteTypeField = getFieldValueByFieldName("siteType", o);
        Map siteType=new HashMap<>();
        siteType.put("I类","");
        siteType.put("II类","");
        siteType.put("III类","");
        siteType.put("IV类","");
        boolean siteTypeBoo = siteType.containsKey(siteTypeField);
        if (siteTypeField != null && !siteTypeBoo) {
            msg.append("场地类型(下拉选项),");
            flag = true;
        }

        // 基础类型(下拉选项)
        String baseTypeField = getFieldValueByFieldName("baseType", o);
        Map baseType=new HashMap<>();
        baseType.put("满堂红","");
        baseType.put("条形基础","");
        baseType.put("砖基础","");
        baseType.put("毛石片石卵石","");
        baseType.put("其他","");
        boolean baseTypeBoo = baseType.containsKey(baseTypeField);
        if (baseTypeField != null && !baseTypeBoo) {
            msg.append("基础类型(下拉选项),");
            flag = true;
        }

        // 设防标准(下拉选项)
        String fortifyStandardField = getFieldValueByFieldName("fortifyStandard", o);
        Map fortifyStandard=new HashMap<>();
        fortifyStandard.put("0（不设防）","");
        fortifyStandard.put("6（VI度设防）","");
        fortifyStandard.put("7（VII度设防）","");
        fortifyStandard.put("8（VIII度设防）","");
        fortifyStandard.put("9（IX度设防）","");
        boolean fortifyStandardBoo = fortifyStandard.containsKey(fortifyStandardField);
        if (fortifyStandardField != null && !fortifyStandardBoo) {
            msg.append("设防标准(下拉选项),");
            flag = true;
        }

        // 规则程度(下拉选项)
        String regularLevelField = getFieldValueByFieldName("regularLevel", o);
        Map regularLevel=new HashMap<>();
        regularLevel.put("1（平立面均规则）","");
        regularLevel.put("2（平面规则立面不规则）","");
        regularLevel.put("3（平面不规则立面规则）","");
        regularLevel.put("4（平面立面均不规则）","");
        boolean regularLevelBoo = regularLevel.containsKey(regularLevelField);
        if (regularLevelField != null && !regularLevelBoo) {
            msg.append("规则程度(下拉选项),");
            flag = true;
        }

        // 承重方式(下拉选项)
        String czTypeField = getFieldValueByFieldName("czType", o);
        Map czType=new HashMap<>();
        czType.put("横墙承重","");
        czType.put("纵墙承重","");
        czType.put("纵横墙承重","");
        czType.put("混合承重","");
        boolean czTypeBoo = czType.containsKey(czTypeField);
        if (czTypeField != null && !czTypeBoo) {
            msg.append("承重方式(下拉选项),");
            flag = true;
        }

        // 承重墙砌筑方式(下拉选项)
        String czqqzTypeField = getFieldValueByFieldName("czqqzType", o);
        Map czqqzType=new HashMap<>();
        czqqzType.put("实心砖墙","");
        czqqzType.put("空斗墙","");
        boolean czqqzTypeBoo = czqqzType.containsKey(czqqzTypeField);
        if (czqqzTypeField != null && !czqqzTypeBoo) {
            msg.append("承重墙砌筑方式(下拉选项),");
            flag = true;
        }

        // 纵横墙交接处墙内孔道(下拉选项)
        String zhqjjcqkdField = getFieldValueByFieldName("zhqjjcqkd", o);
        Map zhqjjcqkd=new HashMap<>();
        zhqjjcqkd.put("无竖向孔道","");
        zhqjjcqkd.put("有竖向孔道","");
        boolean zhqjjcqkdBoo = zhqjjcqkd.containsKey(zhqjjcqkdField);
        if (zhqjjcqkdField != null && !zhqjjcqkdBoo) {
            msg.append("纵横墙交接处墙内孔道(下拉选项),");
            flag = true;
        }

        // 楼梯间布置(下拉选项)
        String ltjbzField = getFieldValueByFieldName("ltjbz", o);
        Map ltjbz=new HashMap<>();
        ltjbz.put("无","");
        ltjbz.put("在尽端或转角处","");
        ltjbz.put("不在尽端或转角处","");
        boolean ltjbzBoo = ltjbz.containsKey(ltjbzField);
        if (ltjbzField != null && !ltjbzBoo) {
            msg.append("楼梯间布置(下拉选项),");
            flag = true;
        }

        // 墙体材质(下拉选项)
        String wallMaterialField = getFieldValueByFieldName("wallMaterial", o);
        Map wallMaterial=new HashMap<>();
        wallMaterial.put("普通砖","");
        wallMaterial.put("多孔砖","");
        wallMaterial.put("砌块","");
        wallMaterial.put("其他","");
        boolean wallMaterialBoo = wallMaterial.containsKey(wallMaterialField);
        if (wallMaterialField != null && !wallMaterialBoo) {
            msg.append("墙体材质(下拉选项),");
            flag = true;
        }

        // 砂浆类型(下拉选项)
        String mortarTypeField = getFieldValueByFieldName("mortarType", o);
        Map mortarType=new HashMap<>();
        mortarType.put("水泥砂浆","");
        mortarType.put("混合砂浆","");
        mortarType.put("泥浆","");
        boolean mortarTypeBoo = mortarType.containsKey(mortarTypeField);
        if (mortarTypeField != null && !mortarTypeBoo) {
            msg.append("砂浆类型(下拉选项),");
            flag = true;
        }

        // 砂浆强度(下拉选项)
        String mortarStrengthField = getFieldValueByFieldName("mortarStrength", o);
        Map mortarStrength=new HashMap<>();
        mortarStrength.put("大于等于M2.5","");
        mortarStrength.put("小于M2.5","");
        boolean mortarStrengthBoo = mortarStrength.containsKey(mortarStrengthField);
        if (mortarStrengthField != null && !mortarStrengthBoo) {
            msg.append("砂浆强度(下拉选项),");
            flag = true;
        }

        // 屋盖类别(下拉选项)
        String roofTypeField = getFieldValueByFieldName("roofType", o);
        Map roofType=new HashMap<>();
        roofType.put("木屋盖","");
        roofType.put("现浇屋盖","");
        roofType.put("预制板","");
        roofType.put("彩钢板","");
        roofType.put("其他","");
        boolean roofTypeBoo = roofType.containsKey(roofTypeField);
        if (roofTypeField != null && !roofTypeBoo) {
            msg.append("屋盖类别(下拉选项),");
            flag = true;
        }

        // 楼盖类别(下拉选项)
        String floorTypeField = getFieldValueByFieldName("floorType", o);
        Map floorType=new HashMap<>();
        floorType.put("木楼板","");
        floorType.put("现浇楼板","");
        floorType.put("预制板","");
        floorType.put("其他","");
        boolean floorTypeBoo = floorType.containsKey(floorTypeField);
        if (floorTypeField != null && !floorTypeBoo) {
            msg.append("楼盖类别(下拉选项),");
            flag = true;
        }

        // 出屋面构件(下拉选项)
        String cwmgjField = getFieldValueByFieldName("cwmgj", o);
        Map cwmgj=new HashMap<>();
        cwmgj.put("0（无）","");
        cwmgj.put("1（女儿墙）","");
        cwmgj.put("2（烟囱）","");
        cwmgj.put("3（无构造柱楼梯间）","");
        cwmgj.put("4（其他）","");
        boolean cwmgjBoo = cwmgj.containsKey(cwmgjField);
        if (cwmgjField != null && !cwmgjBoo) {
            msg.append("出屋面构件(下拉选项),");
            flag = true;
        }

        // 女儿墙设置(下拉选项)
        String parapetInstallField = getFieldValueByFieldName("parapetInstall", o);
        Map parapetInstall=new HashMap<>();
        parapetInstall.put("无女儿墙","");
        parapetInstall.put("女儿墙无拉结","");
        parapetInstall.put("女儿墙有拉结","");
        boolean parapetInstallBoo = parapetInstall.containsKey(parapetInstallField);
        if (parapetInstallField != null && !parapetInstallBoo) {
            msg.append("女儿墙设置(下拉选项),");
            flag = true;
        }

        // 构造柱位置(下拉选项)
        String czzPlaceField = getFieldValueByFieldName("czzPlace", o);
        Map czzPlace=new HashMap<>();
        czzPlace.put("0（未设构造柱）","");
        czzPlace.put("1（在外墙四角、大房间内、外墙交接处、楼梯间及电梯间横墙与纵墙交接处设置构造柱）","");
        czzPlace.put("2（隔开间横墙与外墙交接处设构造柱）","");
        czzPlace.put("3（每开间内外墙交接处设构造柱）","");
        boolean czzPlaceBoo = czzPlace.containsKey(czzPlaceField);
        if (czzPlaceField != null && !czzPlaceBoo) {
            msg.append("构造柱位置(下拉选项),");
            flag = true;
        }

        // 圈梁设置(下拉选项)
        String jlInstallField = getFieldValueByFieldName("jlInstall", o);
        Map jlInstall=new HashMap<>();
        jlInstall.put("0（未设圈梁）","");
        jlInstall.put("1（屋盖处设圈梁）","");
        jlInstall.put("2（隔层设圈梁）","");
        jlInstall.put("3（层层设圈梁）","");
        boolean jlInstallBoo = jlInstall.containsKey(jlInstallField);
        if (jlInstallField != null && !jlInstallBoo) {
            msg.append("圈梁设置(下拉选项),");
            flag = true;
        }

        // 圈梁最大水平间距(下拉选项)
        String jlzdspSpanField = getFieldValueByFieldName("jlzdspSpan", o);
        Map jlzdspSpan=new HashMap<>();
        jlzdspSpan.put("≤8m","");
        jlzdspSpan.put("≤12m","");
        jlzdspSpan.put("≤16m","");
        jlzdspSpan.put(">16m","");
        boolean jlzdspSpanBoo = jlzdspSpan.containsKey(jlzdspSpanField);
        if (jlzdspSpanField != null && !jlzdspSpanBoo) {
            msg.append("圈梁最大水平间距(下拉选项),");
            flag = true;
        }

        // 圈梁闭合情况(下拉选项)
        String jlbhqkField = getFieldValueByFieldName("jlbhqk", o);
        if("0（未设圈梁）".equals(jlInstallField) || "0(未设圈梁)".equals(jlInstallField)){
            if(!StringUtils.isEmpty(jlInstallField)){
                Map jlbhqk=new HashMap<>();
                jlbhqk.put("圈梁闭合","");
                jlbhqk.put("圈梁不闭合","");
                boolean jlbhqkBoo = jlbhqk.containsKey(jlbhqkField);
                if (jlbhqkField != null && !jlbhqkBoo) {
                    msg.append("圈梁闭合情况(下拉选项),");
                    flag = true;
                }
            }
        }else{
            Map jlbhqk=new HashMap<>();
            jlbhqk.put("圈梁闭合","");
            jlbhqk.put("圈梁不闭合","");
            boolean jlbhqkBoo = jlbhqk.containsKey(jlbhqkField);
            if (!jlbhqkBoo) {
                msg.append("圈梁闭合情况(下拉选项),");
                flag = true;
            }
        }


        // 建筑现状(下拉选项)
        String buildingSituationField = getFieldValueByFieldName("buildingSituation", o);
        Map buildingSituation=new HashMap<>();
        buildingSituation.put("完好","");
        buildingSituation.put("墙体歪闪","");
        buildingSituation.put("墙体开裂","");
        buildingSituation.put("墙体腐蚀酥碎","");
        buildingSituation.put("楼屋盖构件有变形","");
        buildingSituation.put("地基不均匀沉降","");
        boolean buildingSituationBoo = buildingSituation.containsKey(buildingSituationField);
        if (buildingSituationField != null && !buildingSituationBoo) {
            msg.append("建筑现状(下拉选项),");
            flag = true;
        }


        //经度
        String longitudeField = getFieldValueByFieldName("longitude", o);
        if(longitudeField!=null && !"".equals(longitudeField)){
            double longitude=Double.valueOf(longitudeField);
            if(longitude>180.0 || longitude<-180.0){
                msg.append("经度,");
                flag = true;
            }
        }
        //纬度
        String latitudeField = getFieldValueByFieldName("latitude", o);
        if(!"".equals(latitudeField) && latitudeField!=null) {
            double latitude = Double.valueOf(latitudeField);
            if (latitude > 90.0 || latitude < -90.0) {
                msg.append("纬度,");
                flag = true;
            }
        }

        return flag;
    }





    /**
     * 根据属性名获取属性值
     *
     * @param fieldName 属性名
     * @param object    对象
     * @return
     */
    private String getFieldValueByFieldName(String fieldName, Object object) {
        try {
            String firstLetter = fieldName.substring(0, 1).toUpperCase();
            String getter = "get" + firstLetter + fieldName.substring(1);
            Method method = object.getClass().getMethod(getter, new Class[]{});
            Object value = method.invoke(object, new Object[]{});
            return value.toString();
        } catch (Exception e) {
            return null;
        }
    }
}
